package fonkin.com.wifisignapp.adapter.news;

import android.content.Context;
import android.graphics.Bitmap;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;


import com.squareup.picasso.Picasso;

import java.util.Collection;
import java.util.List;

import fonkin.com.wifisignapp.R;
import fonkin.com.wifisignapp.bean.news.NewsInfor;
import fonkin.com.wifisignapp.constants.AppConstants;

import static com.squareup.picasso.MemoryPolicy.NO_CACHE;
import static com.squareup.picasso.MemoryPolicy.NO_STORE;

/**
 * @Description:新闻列表Adapter
 * @Author: huanghongjing
 * @Date: 2018/4/7 23:43
 * No such property: code for class: Script1
 *
 */

public class NewsAdapter extends BaseAdapter {
    private Context context;
    private List<NewsInfor> newsInfoList;

    public NewsAdapter(Context context, List<NewsInfor> newsInfoList) {
        this.context = context;
        this.newsInfoList = newsInfoList;
    }

    @Override
    public int getCount() {
        return newsInfoList.size();
    }

    @Override
    public Object getItem(int position) {
        return newsInfoList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }


    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView==null){
            convertView = LayoutInflater.from(context).inflate(R.layout.common_news_item,parent,false);
            convertView.setTag(new Viewholder(convertView));
        }
        Viewholder tag = (Viewholder) convertView.getTag();

        NewsInfor newsInfor = newsInfoList.get(position);
        tag.newsTitle.setText("标题："+newsInfor.getTitle());
        tag.publisher.setText("发布者："+newsInfor.getDeptName());
        tag.publishTime.setText("发布时间："+newsInfor.getPublishTime());

        Picasso
                .with(context)
                .load(AppConstants.IMAGE_API+newsInfor.getThumbnail())
                .placeholder(R.drawable.loading_pic)
                .resize(80,50)
                .error(R.drawable.no_pic)
                .memoryPolicy(NO_CACHE, NO_STORE)
                .config(Bitmap.Config.RGB_565)
                .onlyScaleDown()
                .into(tag.imag);
        return convertView;
    }

    public void addAll(Collection<? extends NewsInfor> collection){
        newsInfoList.addAll(collection);
        notifyDataSetChanged();
    }
    public static  class Viewholder{
        private final ImageView imag;
        private final TextView newsTitle,publisher,publishTime;

        public  Viewholder(View item){
            imag = ((ImageView) item.findViewById(R.id.news_thumbail_img));
            newsTitle = ((TextView) item.findViewById(R.id.news_title));
            publisher = ((TextView) item.findViewById(R.id.publisher));
            publishTime = ((TextView) item.findViewById(R.id.publish_time));

        }
    }
}
